﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.ExchangeAudit
{
    public class ExchangeAuditDal_ZD
    {
        public string GetMaxCode()
        {
            string sql = @"select isnull(max(Code),0) from t5715_ExchangeAudit_ZD";
            Database db = DatabaseFactory.CreateDatabase();
            object maxCode = db.ExecuteScalar(CommandType.Text, sql);

            return maxCode.ToString();

        }

        /// <summary>
        /// 生成流水号
        /// </summary>
        /// <returns></returns>
        public string CreateApplyCode()
        {
            return GetSerialNumber(GetMaxCode());
        }

        public string GetSerialNumber(string serialNumber)
        {
            if (serialNumber != "0")
            {
                string headDate = serialNumber.Substring(0, 6);
                int lastNumber = int.Parse(serialNumber.Substring(6));
                //如果数据库最大值流水号中日期和生成日期在同一天，则顺序号加1
                if (headDate == DateTime.Now.ToString("yyMMdd"))
                {
                    lastNumber++;
                    return headDate + lastNumber.ToString("0000");
                }
            }
            return DateTime.Now.ToString("yyMMdd") + "0001";
        }
    }
}